import 'package:asperm/tools/widget_extension.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class MyDialog extends Dialog {
  String title;
  String describe;
  String leftStr;
  String rightStr;
  double width;
  double height;

  Function() cancelFun;
  Function() sureFun;

  MyDialog(
      {this.title = '',
      this.describe = '',
      this.leftStr = '',
      this.rightStr = '',
      this.width = 320,
      this.height = 200,
      required this.cancelFun,
      required this.sureFun});

  @override
  Widget build(BuildContext context) {
    Color sureColor = const Color.fromRGBO(224, 224, 224, 1);
    return Material(
        type: MaterialType.transparency,
        child: Center(
            child: Container(
              width: width,
              decoration: const BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(24))),
              child: Column(mainAxisAlignment: MainAxisAlignment.start,mainAxisSize: MainAxisSize.min,children: [
                Container(
                  margin: EdgeInsets.only(top: 25.h, bottom: 12,left: 20.w,right: 20.w),
                  child: Text(title,
                      style: TextStyle(
                          fontSize: 20.sp, fontWeight: FontWeight.w600,)),
                ),
                // Padding(
                //     padding: const EdgeInsets.only(top: 36, bottom: 12),
                //     child: ),
                Container(
                  margin: EdgeInsets.only(bottom: 20.h,left: 20.w,right: 20.w),
                  child: Text(
                      describe,
                      style: TextStyle(
                          fontSize: 14.sp, fontWeight: FontWeight.w400),
                  ),
                ),
                // Padding(
                //     padding: const EdgeInsets.only(bottom: 20),
                //     child: Text(describe)),
                Container(
                  margin: EdgeInsets.only(bottom: 20.h),
                  height: 40.h,
                  child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        Container(
                          width: 110.w,
                          height: 40.h,
                          decoration: BoxDecoration(
                              color: Color(0xffE9E9E9),
                              borderRadius: BorderRadius.circular(19),
                              border: Border.all(width: 1, color: sureColor)),
                          child: TextButton(
                            style: ButtonStyle(
                              shape: MaterialStateProperty.all<
                                  RoundedRectangleBorder>(
                                RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(20.h),
                                ),
                              ),
                            ),
                            child: Text(leftStr,
                                style: TextStyle(
                                    color: Color.fromRGBO(51, 51, 51, 1),
                                    fontSize: 16.sp,
                                )),
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                          ),
                        ),
                        Container(
                          width: 110.w,
                          height: 40.h,
                          decoration: BoxDecoration(
                            color: const Color.fromRGBO(255, 94, 68, 1),
                            borderRadius: BorderRadius.circular(20.h),
                          ),
                          child: TextButton(
                            style: ButtonStyle(
                              shape: MaterialStateProperty.all<
                                  RoundedRectangleBorder>(
                                RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(20.h),
                                ),
                              ),
                            ),
                            child: Text(
                              rightStr,
                              style: TextStyle(
                                  color: Colors.white, fontSize: 16.sp,),
                            ),
                            onPressed: () {
                              sureFun();
                            },
                          ),
                        )
                      ]),
                )
              ]),
            )));
  }
}

class customAlertDialog extends Dialog {
  String title;
  String describe;
  String leftStr;
  String rightStr;
  String hintString;
  Widget? contentWidgte;

  double width = 320.w;
  double height = 200.h;
  bool isEmailLogin = true;
  bool inputVisible = false;
  TextEditingController? accountController = TextEditingController();
  Function() cancelFun;
  Function() sureFun;
  Function(String)? onTextChanged;

  customAlertDialog(
      {this.title = '',
      this.describe = '',
      this.leftStr = '',
      this.rightStr = '',
      this.hintString = '',
      this.isEmailLogin = true,
      this.inputVisible = false,
        this.contentWidgte,
        this.accountController,
      required this.cancelFun,
      required this.sureFun,
        this.onTextChanged});

  @override
  Widget build(BuildContext context) {
    Color sureColor = const Color.fromRGBO(224, 224, 224, 1);
    return Center(
      child: Container(
        width: 320.w,
        padding:
            EdgeInsets.only( bottom: 20.h),
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(8.w))),
        child: Column(mainAxisSize: MainAxisSize.min, children: [
          Visibility(
            visible: (title == '') ? false : true,
            child: Padding(
                padding: EdgeInsets.only(top: 20.h,left: 20.w, right: 20.w,bottom: describe == '' ? 20.h : 10.h),
                child: Text(title,
                    style: TextStyle(fontSize: 20.sp,color: Color(0xff1B1B1B), fontWeight: FontWeight.w600),
                  textAlign: TextAlign.center,
                )
            ),
          ),
          Visibility(
            visible:contentWidgte != null ? true : (describe == '') ? false : true,
            child: Padding(
                padding: EdgeInsets.only(
                     bottom: 20.h, left: 20.w, right: 20.w),
                child: contentWidgte != null ? contentWidgte : Text(
                  describe,
                  style: TextStyle(
                    letterSpacing: 0,
                      fontSize: 14.sp,
                      color: Color(0xff393939),
                      fontWeight: FontWeight.w400,),
                )),
          ),
          Visibility(
            visible: inputVisible,
            child: Padding(
                padding: EdgeInsets.only(left: 30.w, right: 30.w, bottom: 30.h),
                child: Container(
                  // margin: const EdgeInsets.fromLTRB(20, 0, 20, 0),
                  height: 40,
                  // decoration: BoxDecoration(
                  //   color: Colors.white,
                  //   borderRadius: BorderRadius.all(Radius.circular(16)),
                  //   border: Border.all(
                  //     color: Color(0xFFE4C8B6), // 边框颜色
                  //     width: 1, // 边框宽度
                  //   ),
                  // ),
                  child: TextField(
                    controller: accountController,
                    onChanged: onTextChanged,
                    decoration: InputDecoration(
                      // border: InputBorder.none,
                      enabledBorder: UnderlineInputBorder(
                        borderSide:
                            BorderSide(color: Color(0xFFD8D8D8)), // 下划线颜色
                      ),
                      focusedBorder: UnderlineInputBorder(
                        borderSide:
                            BorderSide(color: Color(0xFFD8D8D8)), // 获得焦点时下划线颜色
                      ),
                      hintText: hintString,
                      contentPadding: EdgeInsets.only(
                          bottom: 0.h, left: 5.w), // 调整文字位置以垂直居中
                    ),
                    cursorColor: Color(0xFFffED6D20),
                    keyboardType: TextInputType.emailAddress,
                  ),
                )),
          ),
          Row(mainAxisAlignment:leftStr == '' ?  MainAxisAlignment.center : MainAxisAlignment.spaceEvenly, children: [
            Visibility(
                visible: (leftStr == '') ? false : true,
                child: Flexible(
                  child: Container(
                    width: 100.w,
                    height: 40.h,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(20.h),
                        // border: Border.all(width: 1, color: sureColor),
                        color: Color(0xffE9E9E9),
                    ),
                    child: Text(leftStr,
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          color: Color(0xff5A5A5A),
                          fontSize: 20.sp,
                          fontWeight: FontWeight.w400,
                        )),
                  ).gestureDetector(
                    onTap: (){
                      accountController?.clear();
                      cancelFun();
                    }
                  ),
                ),
            ),
            Container(
              width: 100.w,
              height: 40.h,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Color(0XffED6D20),
                borderRadius: BorderRadius.circular(20.h),
              ),
              child: Text(
                rightStr,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 20.sp,
                  fontWeight: FontWeight.w400,
                ),
                maxLines: 2, // 限制文本显示的最大行数为2
                overflow: TextOverflow.ellipsis,
              ),
            ).gestureDetector(
                onTap: (){
                  accountController?.clear();
                  sureFun();
                }
            ),
          ])
        ]),
      ),
    );
  }
}

class TakePicDialog extends Dialog {
  String title;
  String describe;
  String okStr;
  double width;
  double height;
  Function() sureFun;

  TakePicDialog(
      {this.title = '',
      this.describe = '',
      this.okStr = '',
      this.width = 320,
      this.height = 250,
      required this.sureFun});

  @override
  Widget build(BuildContext context) {
    Color sureColor = const Color.fromRGBO(224, 224, 224, 1);
    return Center(

      child: Container(
          margin: EdgeInsets.only(left: 20.w,right: 20.w),
          decoration: const BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(24))),
          child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
            Container(
                padding: EdgeInsets.only(top: 20.h, bottom: 12.h),
                child: Text(title,
                  style: TextStyle(fontSize: 24.sp, fontWeight: FontWeight.bold,),
                  textAlign: TextAlign.center,
                )
            ),
            Container(
                padding: EdgeInsets.only(top: 10.h, bottom: 20.h, left: 20.w, right: 20.w),
                child: Text(
                  describe,
                  style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.normal),
                )),
            Center(
              child: Container(
                width: 100.w,
                height: 44.h,
                margin: EdgeInsets.only( bottom: 20.h,),
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(19),
                    border: Border.all(width: 1, color: sureColor)),
                child: TextButton(
                  style: ButtonStyle(
                    shape: MaterialStateProperty.all<
                        RoundedRectangleBorder>(
                      RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(19.0),
                      ),
                    ),
                  ),
                  child: Text(okStr,
                      style: TextStyle(
                          color: Color(0xff5A5A5A),
                          fontSize: 16.sp,
                      )),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
              ),
            )
          ]))
    );
  }
}
